<?php
namespace app\web\admin;

use app\common\builder\ZBuilder;
use app\web\model\Project as myModel;
use app\web\model\Category;
use app\web\model\Shop;
use app\web\model\Technician;
use app\web\model\Reason;




/**
 * 服务项目
 * @package
 */
class Project extends Base 
{
    public $check_status = ['待审核','通过','未通过'];
    public function index()
    {
        $map = $this->getMap();
  
        foreach ($map as $key => $value) {
            if($value[0] == 'shop_id') $map[$key][0] = 'a.shop_id';
            if($value[0] == 'status') $map[$key][0] = 'a.status';
            if($value[0] == 'check_status') $map[$key][0] = 'a.check_status';
            if($value[0] == 'technician_id') $map[$key][0] = 'a.technician_id';
            if($value[0] == 'is_recommend') $map[$key][0] = 'a.is_recommend';
        }
        $data_list = myModel::alias('a')
                        ->where($map)
                        ->leftjoin('category d','d.id=a.category_id')
                        ->leftjoin('technician b','b.id=a.technician_id')
                        ->leftjoin('shop c','c.id=a.shop_id')
                        ->field('a.id,a.project_name,a.position_name,a.service_time,a.money,a.order_number,a.status,a.check_status,a.is_recommend,d.category_name as category_id,ifnull(b.nickName,"") as technician_id,ifnull(c.shop_name,"") as shop_id,a.create_time')
                        ->order($this->getOrder('a.create_time desc'))
                        // ->select();
                        ->paginate();
        return ZBuilder::make('table')
            ->setTableName('project')
            ->hideCheckBox()
            ->assign('count',myModel::where('check_status',0)->count())
            ->addColumns([
                ['id','ID'],
                ['category_id','项目分类'],
                ['technician_id','技师昵称'],
                ['shop_id','店铺名称'],
                ['project_name','项目名称'],
                ['position_name','服务部位','callback',function($v){
                    return implode(',', $v);
                }],
                ['service_time','服务时长(分钟)'],
                ['money','服务金额'],
                ['order_number','服务次数'],
                ['is_recommend','是否推荐','status','',['否','是']],
                ['status','是否上架','status','',['否','是']],
                ['check_status','平台审核状态','status','',$this->check_status],
                ['create_time','提交时间'],
                ['right_button', '操作', 'btn']
            ])
            ->setRowList($data_list)
            ->setSearch(['project_name' => '项目名称','id'=>'ID']) // 设置搜索参数
            ->addOrder('money,order_number,service_time')
            ->addFilterList('status',['下架','上架'])
            ->addFilterList('is_recommend',['否','是'])
            ->addFilterList('check_status',$this->check_status)
            ->addFilterList('technician_id',Technician::getSelectAll('check_status=1','nickName'))
            ->addFilterList('shop_id',Shop::getSelectAll('check_status=1','shop_name'))
            ->addFilterList('category_id',Category::getSelectAll('category_type=0'))
            // ->addTrClass('check_status0', 'check_status', '=', 0)
            // ->setExtraCss($this->getCss())
            ->addRightButtons(['edit'=>['title'=>'查看'],'check'=>['title'=>'审核','href'=>url('check',['id'=>'__id__'])]])
            // ->replaceRightButton(['check_status' => ['in','1,2']], '','check') // 修改id为1的按钮
            ->fetch();
    }
    public function check($id)
    {
        $reason = Reason::getSelectAll('status=1','reason');
        $info = myModel::get($id);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if($data['check_status'] == 0) {
                $data['all_status'] = 0;
            }elseif($data['check_status'] == 2){
                if(!$data['check_desc']) $this->error('请选择不予通过原因');
                $data['check_desc'] = $reason[$data['check_desc']];
                $data['all_status'] = 2;
            }else{
                if($info['shop_id'] == 0){
                    $data['all_status'] = 1;
                }
            }
            $t = Technician::get($info['technician_id']);
            
            $data['check_time'] = $this->request->time();
            $data['check_uid'] = UID;
            $data['update_time'] = $this->request->time();
            if (myModel::where('id',$data['id'])->update($data)) {
                myModel::setIsProject($data['id']);
                $this->success('操作成功','index');
            } else {
                $this->error('操作失败');
            }
        }
        
       return ZBuilder::make('form')
                    ->addFormItems([
                        ['hidden','id'],
                        ['radio','check_status','审核状态','',$this->check_status],
                        ['select','check_desc','不予通过原因','',$reason],
                    ])
                    ->setTrigger('check_status','2','check_desc')
                    ->setFormData($info)
                    ->fetch();

    }
    public function edit($id = 0)
    {
        $info = myModel::alias('a')
                        ->where('a.id',$id)
                        ->leftjoin('category d','d.id=a.category_id')
                        ->leftjoin('technician b','b.id=a.technician_id')
                        ->leftjoin('shop c','c.id=a.shop_id')
                        ->field('a.id,a.project_name,a.position_name,a.service_time,a.money,a.order_number,a.status,a.check_status,a.is_recommend,d.category_name as category_id,ifnull(b.nickName,"") as technician_id,ifnull(c.shop_name,"") as shop_id,a.image,a.project_images,a.project_desc')
                        ->find()->toArray();
        $info['position_name'] = $info['position_name'] ? implode(',',$info['position_name']) : '';
        $info['check_status1'] = $this->check_status[$info['check_status']];
        $info['is_recommend1'] = ['否','是'][$info['is_recommend']];
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','id'],
                ['static:6','category_id','项目分类'],
                ['static:6', 'project_name', '项目名称'],
                ['static:6','technician_id','所属技师'],
                ['static:6','shop_id','所属店铺'],
                ['static:6', 'service_time', '服务时长(分钟)'],
                ['static:6', 'money', '服务金额'],
                ['static:6', 'order_number', '服务次数'],
                ['static:6','position_name','服务部位'],
                ['static:6','check_status1','审核状态'],
                ['static:6','is_recommend1','是否推荐'],
                ['static','project_desc','项目介绍'],
                ['gallery','image','项目图片'],
                ['gallery','project_images','项目更多图片'],
            ])
            ->hideBtn('submit')
            ->setFormData($info)
            ->fetch();
    }
}